package com.workspace.chb_website.controller;

import com.workspace.chb_website.bean.User;
import com.workspace.chb_website.bean.RespBean;
import com.workspace.chb_website.bean.MetaBean;
import com.workspace.chb_website.service.UserService;
import com.workspace.chb_website.util.Util;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 登录注册相关控制
 */

@CrossOrigin
@RestController
@Api(tags = "登录相关接口")
public class LoginController {

    private final UserService userService;

    @Autowired
    public LoginController(UserService userService) {
        this.userService = userService;
    }

    @ApiOperation("未登录页面，请求需要登陆的资源时会跳转到这个页面")
    @GetMapping("/login_page")
    public RespBean loginPage() {
        MetaBean meta = new MetaBean(400, "长时间未操作，请重新登录!");
        return new RespBean("", meta);
    }

    @ApiOperation("修改密码接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "oldPassword", value = "旧密码", required = true, dataType = "String"),
            @ApiImplicitParam(name = "newPassword", value = "新密码", required = true, dataType = "String")})
    @PostMapping("/updatePassword")
    public RespBean updatePassword(HttpServletRequest request) throws ServletRequestBindingException {
        User currentUser = Util.getCurrentUser();
        String oldPassword = ServletRequestUtils.getStringParameter(request, "oldPassword");
        String newPassword = ServletRequestUtils.getStringParameter(request, "newPassword");
        return userService.updatePassword(currentUser, oldPassword, newPassword);
    }
}
